*Clean and Organize Estimates

clear all
local indus "151 153 154 155 181 201 221 222 242 281"
foreach ind of local indus  {
use markup_`ind', clear

*Generate additional variables
capture gen double adrat1 = ad_pq_it / f1_oper_rev_it
capture gen double adrat2 = ad_pq_it / f1_sale_it

*Drop variables
replace ego = ego_spf if ego == .
replace logprod = logprod_spf if logprod == .
replace prod = prod_spf if prod == .
replace qg_mpf = qg if qg_mpf == .
drop qg
rename qg_mpf qg

order flag* ad_pq_it
drop p_ft*
drop prod_obs pp qg_1 - p_1 
drop f3_q_prod_ijtu - labor_it electr_pq_it - f1_pct_add_rev_it
sort CIIU3_3D id time cpc3
order CIIU3_3D id time cpc3 markup* flag*

*Reshape the data so observations are at the firm/time/product and /destination level
*----Reshape markup, quantity exported and price
save temp, replace
keep CIIU3 id time cpc3 markup* q_fob_x_ijtu*  p_fjt*
reshape long markup_ q_fob_x_ijtu_ p_fjt_, i(CIIU3 id time cpc3) j(dest) string


*Combine markups from single product firms (spf) and multi-product firms
split dest, p("_")
rename dest dest_old
rename dest2 spf_indic
ta spf, m
rename dest1 dest
*drop spf_indic

save temp2, replace

*Merge reshaped data back into main dataset (containing all the other variables)
use temp, clear
drop markup_* q_fob_x_ijtu* p_fjt*
sort CIIU3_3D id time cpc3
merge 1:m CIIU3_3D id time cpc3 using temp2
drop _m

bys id time cpc3 dest: egen m_mark = mean(markup)
bys id time cpc3 dest: egen m_q_fob = mean(q_fob_x_ijtu)
bys id time cpc3 dest: egen m_p_fjt = mean(p_fjt)
drop markup_ q_fob_x_ijtu_ p_fjt_
rename m_mark  markup
rename m_q_fob q_fob_x_ijtu
rename m_p_fjt p_fjt

drop if spf == "spf"
drop spf
drop if markup == .

***Form quantity variables at ftpd level
rename y_fjt_dom dom_quant 
gen double quant = q_fob_x_ijtu
replace quant = dom_quant if dest == "dom"
gen double l_quant = ln(quant)
drop q_fob_x_ijtu dom_quant

*Rename variables
rename r r_tot
rename l_rr_fjt r   
replace logprod = logprod_spf if logprod == . & logprod_spf ~= .
drop logprod_spf

gen l_q_prod_ijtu = ln(q_prod_ijtu)
gen l_q_exp_ijtu = ln(q_exp_ijtu)
gen l_q_sale_ijt = ln(q_sale_ijt)

#delimit ;
order CIIU3_3D id time cpc3 num_prod dest markup logprod 
      r l k i 
      r_tot l_tot k_tot i_tot 
	  q_prod_ijtu q_exp_ijtu q_sale_ijt
	  l_q_prod_ijtu l_q_exp_ijtu l_q_sale_ijt
	  p_fjt quant
	  ad_pq_it adrat1 adrat2;
#delimit cr


**Re-scale variables to original units
replace i = i + 5
replace l = l + 3
foreach var of varlist r k l i    {
   replace `var' = `var' + ln(1000)
   }
   
*Label Variables
label variable dest "destination of exports---ftpd"
label variable num_prod "number of products sold--ft"
label variable markup "markup--ftpd"
label variable logprod "log of TFP--ftp"
label variable r "revenues--ftp"
label variable l "labor--ftp"
label variable k "capital--ftp"
label variable i "intermediate inputs--ftp"

label variable r_tot "revenues--ft"
label variable l_tot "labor--ft"
label variable k_tot "capital--ft"
label variable i_tot "intermediate inputs--ft"

label variable q_prod_ijtu "quantity produced--ftp--LEVELS"
label variable q_exp_ijtu "quantity exported--ftp--LEVELS"
label variable q_sale_ijt "quantity sold--ftp--LEVELS"
label variable quant "quantity sold--ftpd--LEVELS"

label variable l_q_prod_ijtu "quantity produced--ftp"
label variable l_q_exp_ijtu "quantity exported--ftp"
label variable l_q_sale_ijt "quantity sold--ftp"
label variable l_quant "quantity sold--ftpd"

label variable p_fjt "price--ftpd"

label variable flag_p "price is in top/bottom 2.5% for that product--ftp"
label variable flag_p_dom "DOM: price is in top/bottom 2.5% for that product--ftp"

label variable adrat1 "Advertising Ratio--Operating Revenue"
label variable adrat2 "Advertising Ratio--Sales"


#delimit ;
keep CIIU3_3D id time cpc3 num_prod dest markup logprod 
      r l k i 
      r_tot l_tot k_tot i_tot 
	  rwl hl 
	  w_total_white_c_it hours_labor_white_c_it 
	  w_total_blue_c_it  hours_labor_blue_c_it
	  white_blue_wage_ratio
	  q_prod_ijtu q_exp_ijtu q_sale_ijt
	  l_q_prod_ijtu l_q_exp_ijtu l_q_sale_ijt
	  p_fjt quant l_quant
	  flag* dexp_ftp
	  ad_pq_it adrat1 adrat2 ai al ak
	  ielas kelas lelas int_input_price ego;
#delimit cr
sort id time cpc3 dest
save markups_`ind'_clean, replace
}


*Compile Estimates Across Industries
use markups_151_clean, clear
local indus "153 154 155 181 201 221 222 242 281"
foreach ind of local indus  {
	append using markups_`ind'_clean
}


*Create Additional Variables for Empirical Analysis
sort id time cpc3 dest
encode dest, gen(dest_num)

gen l_markup = ln(markup)
gen mult_prod = num_prod > 1
replace mult_prod = . if num_prod == .

*Marginal Cost
gen mc_ftpd = p_fjt / markup


*Compute a and b from markup estimates

* For markups below one, compute a modified value of b
gen double markup_orig = markup
replace markup = 1 + 0.01 if markup <= 1	
gen double b = (p_fjt -p_fjt*markup) / (quant * markup)	
drop markup
ren markup_orig markup		

gen double a = p_fjt - b * quant


*Compute product ranks
preserve
gen r_ftpd = p_fjt * quant
collapse (sum) r_ftpd, by(id time cpc3)
rename r_ftpd sum_rev_ftp
bys id time: egen double rank = rank(-sum_rev_ftp)
sort id time cpc3
save temp_prod
restore
sort id time cpc3
merge id time cpc3 using temp_prod
erase temp_prod.dta
drop _merge

ta rank, gen(d_rank)


save markups_for_analysis, replace
